package com.wehealth;


import java.util.ArrayList;
import java.util.List;

import com.wehealth.adapter.Adapter_questionnaire_page2;
import com.wehealth.adapter.Adapter_questionnaire_page3;
import com.wehealth.adapter.Adapter_questionnaire_page4;
import com.wehealth.adapter.Adapter_questionnaire_page4_result;
import com.wehealth.dm.DM_Questionnaire_page2_item;
import com.wehealth.dm.DM_Questionnaire_page3_item;
import com.wehealth.util.QuestionnaireDataManager;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


@SuppressLint("ValidFragment")
public class Questionnaire_page4 extends Fragment {

	private MainActivity context;
	private QuestionnaireDataManager  mQuestionnaireDataManage;
	
	private  List<String>  mResultListStrs;

	public  Questionnaire_page4()
	{
		Log.i("init:", "Message"); 

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		return  inflater.inflate(R.layout.questionnaire_page4, null);
	}
	
	
	
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
		
		context =(MainActivity)getActivity();
		mResultListStrs = new ArrayList<String>();
		
		

		mQuestionnaireDataManage = QuestionnaireDataManager.getInstance();	
		List<String>  mResultPage3 = mQuestionnaireDataManage.getStringList();
		
		
        TextView reusltScore = (TextView)context.findViewById(R.id.questionnaire_page4_result_score);
        int score =mQuestionnaireDataManage.getScore();
        String scoreStr = String.valueOf(score);
        reusltScore.setText(scoreStr);
        Log.d("SIMMON","  score=  "+score);
        float persent = (float) ((float)score/1000);
        
        ImageView  flag_icon = (ImageView)context.findViewById(R.id.questionnaire_page4_score_flag_icon);
        float  padding =  context.getResources().getDimension(R.dimen.questionnaire_page4_margin);
        int screenWidth = context.getWindowManager().getDefaultDisplay().getWidth(); 
        Log.d("SIMMON","  padding=  "+padding);
        Log.d("SIMMON","  screenWidth=  "+screenWidth);
        
        float   zoonwidth = screenWidth -padding*2;
        
        float  flag_icon_x = zoonwidth*persent;
        
        Log.d("SIMMON","  zoonwidth=  "+zoonwidth);
        Log.d("SIMMON","  flag_icon_x=  "+flag_icon_x);
        Log.d("SIMMON","  persent=  "+persent);
        flag_icon.setX(flag_icon_x);
        
        TextView  healthy_plan = (TextView)context.findViewById(R.id.questionnaire_page4_result_plan);
        String  subhealthy_plan = getString(R.string.questionaire_page4_recommend_project1);
        String  reuslt1 = null;
        if(score >600 && score <= 800){
        	healthy_plan.setText(subhealthy_plan);
        	reuslt1 = getString(R.string.questionaire_page4_result2);
        }else if(score >800){
        	reuslt1 = getString(R.string.questionaire_page4_result3);
        }else if(score < 600){
        	reuslt1 = getString(R.string.questionaire_page4_result1);
        }
        
        mResultListStrs.add(reuslt1);
        
        int  length = mResultPage3.size();
        if(length > 0){
	        for(int k = 0; k < length;k++){
	        	mResultListStrs.add(mResultPage3.get(k));
	        }
        }
        
        
        
        
        String[] planList = context.getResources().getStringArray(R.array.questionnaire_page4_plan_list);
        
        Adapter_questionnaire_page4 adapter = new Adapter_questionnaire_page4(context, planList);
        
        ListView   mListView = (ListView)context.findViewById(R.id.questionnair_page4_list_plan);
        mListView.setAdapter(adapter);
        
        ListView  mListViewResult =(ListView)context.findViewById(R.id.questionnair_page4_list_result);
        
        Adapter_questionnaire_page4_result adapter2 = new Adapter_questionnaire_page4_result(context, mResultListStrs);
        mListViewResult.setAdapter(adapter2);
        

	}

	
	private void openDialog(){
		
		        AlertDialog.Builder builder = new AlertDialog.Builder(context); 
		        builder.setIcon(null);
		        String title = getString(R.string.questionaire_page3_dialog);
		        TextView textview = new TextView(context);
		         textview.setText(title);
		         textview.setTextColor(Color.BLACK);
		         textview.setTextSize(16);
		         textview.setPadding(5, 5, 5, 5);
		         textview.setGravity(Gravity.CENTER_VERTICAL);
		         builder.setView(textview);
		         builder.setPositiveButton("查看结果", new AlertDialog.OnClickListener() {
		             @Override
		             public void onClick(DialogInterface arg0, int arg1) {
		                 // TODO Auto-generated method stub

		             }
		             
		         });
		         builder.create().show();
		         
		     }

}
